using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.SS.Util;
using NPOI.XSSF.UserModel;
using Catastrophe.Common.ObjM;

namespace Catastrophe.Common.Tools.Excel
{
	public class NpoiExcel
	{
		public static bool Export(int startRow, int maxRow, int colCount, Func<int, bool> checkCol, Func<int, int, string> fancGetCellValue, Func<int, string> fancGetHeaderValue, string filePath, string sheetName = "sheet1", Image img = null, NpoiCellConfig nopiCellConfig = null, Action<int> calProcess = null, Func<int, int, NopiCellStyle> cellStyleCallBack = null)
		{
			try
			{
				if (nopiCellConfig == null)
				{
					nopiCellConfig = new NpoiCellConfig();
				}
				ControlLogic<int> controlLogic = new ControlLogic<int>();
				Dictionary<int, ICellStyle> dictionary = new Dictionary<int, ICellStyle>();
				HSSFWorkbook hSSFWorkbook = new HSSFWorkbook();
				ISheet sheet = hSSFWorkbook.CreateSheet(sheetName);
				sheet.DefaultRowHeightInPoints = nopiCellConfig.RowHeight;
				sheet.DefaultColumnWidth = nopiCellConfig.ColWidth;
				IRow row = sheet.CreateRow(0);
				ICellStyle defualtStyle = hSSFWorkbook.CreateCellStyle();
				HSSFFont hSSFFont = (HSSFFont)hSSFWorkbook.CreateFont();
				if (nopiCellConfig.CellStyle == null)
				{
					defualtStyle.Alignment = HorizontalAlignment.Left;
					defualtStyle.VerticalAlignment = VerticalAlignment.Center;
					hSSFFont.Color = 18;
					defualtStyle.FillForegroundColor = 9;
					hSSFFont.Boldweight = 700;
					defualtStyle.WrapText = true;
					defualtStyle.SetFont(hSSFFont);
				}
				else
				{
					NpoiExcel.CellFormat(ref defualtStyle, hSSFFont, nopiCellConfig.CellStyle);
				}
				int num = 0;
				int num2 = 0;
				while (num < colCount)
				{
					if (!checkCol(num))
					{
						num2--;
					}
					else
					{
						string cellValue = fancGetHeaderValue(num);
						row.CreateCell(num2, CellType.String).SetCellValue(cellValue);
						row.HeightInPoints = nopiCellConfig.RowHeight;
						row.GetCell(num2).CellStyle = defualtStyle;
					}
					num++;
					num2++;
				}
				if (nopiCellConfig.IsMerged)
				{
					sheet.AddMergedRegion(new CellRangeAddress(nopiCellConfig.NopiMergedConfig.FirstRow, nopiCellConfig.NopiMergedConfig.LastRow, nopiCellConfig.NopiMergedConfig.FirstCol, nopiCellConfig.NopiMergedConfig.LastCol));
				}
				for (int i = startRow; i < maxRow; i++)
				{
					IRow row2 = sheet.CreateRow(i);
					row2.HeightInPoints = nopiCellConfig.RowHeight;
					int num3 = 0;
					int num4 = 0;
					while (num3 < colCount)
					{
						if (!checkCol(num3))
						{
							num4--;
						}
						else
						{
							string cellValue2 = fancGetCellValue(i, num3);
							row2.CreateCell(num4, CellType.String).SetCellValue(cellValue2);
							NopiCellStyle nopiCellStyle = cellStyleCallBack?.Invoke(i, num3);
							if (nopiCellStyle != null)
							{
								if (!dictionary.ContainsKey(nopiCellStyle.Index))
								{
									ICellStyle defualtStyle2 = hSSFWorkbook.CreateCellStyle();
									defualtStyle2.FillPattern = FillPattern.SolidForeground;
									HSSFFont defualtFont = (HSSFFont)hSSFWorkbook.CreateFont();
									NpoiExcel.CellFormat(ref defualtStyle2, defualtFont, nopiCellStyle);
									dictionary.Add(nopiCellStyle.Index, defualtStyle2);
								}
								row2.GetCell(num4).CellStyle = dictionary[nopiCellStyle.Index];
							}
						}
						num3++;
						num4++;
					}
					int num5 = i * 100 / maxRow;
					if (controlLogic.IsDataChange(num5))
					{
						calProcess?.Invoke(num5);
					}
				}
				if (img != null)
				{
					MemoryStream memoryStream = new MemoryStream();
					img.Save(memoryStream, ImageFormat.Gif);
					byte[] pictureData = memoryStream.ToArray();
					int pictureIndex = hSSFWorkbook.AddPicture(pictureData, PictureType.JPEG);
					HSSFPatriarch hSSFPatriarch = (HSSFPatriarch)sheet.CreateDrawingPatriarch();
					HSSFClientAnchor anchor = new HSSFClientAnchor(nopiCellConfig.NopiImgConfig.DxLeft, nopiCellConfig.NopiImgConfig.DyTop, nopiCellConfig.NopiImgConfig.DxRight, nopiCellConfig.NopiImgConfig.DyDown, nopiCellConfig.NopiImgConfig.StartColTop, nopiCellConfig.NopiImgConfig.StarRowTop, nopiCellConfig.NopiImgConfig.EndColDown, nopiCellConfig.NopiImgConfig.EndRowDown);
					HSSFPicture hSSFPicture = (HSSFPicture)hSSFPatriarch.CreatePicture(anchor, pictureIndex);
				}
				using (Stream @out = File.OpenWrite(filePath))
				{
					hSSFWorkbook.Write(@out);
				}
				calProcess?.Invoke(100);
				return true;
			}
			catch (Exception innerException)
			{
				throw new Exception("导出失败！", innerException);
			}
		}

		public static bool Export<T>(List<T> list, string filePath, string sheetName, Image img = null, NpoiCellConfig nopiCellConfig = null, Action<int> calProcess = null, Func<int, int, NopiCellStyle> cellStyleCallBack = null)
		{
			try
			{
				if (nopiCellConfig == null)
				{
					nopiCellConfig = new NpoiCellConfig();
				}
				if (list == null || list.Count == 0)
				{
					return false;
				}
				ControlLogic<int> controlLogic = new ControlLogic<int>();
				Dictionary<int, ICellStyle> dictionary = new Dictionary<int, ICellStyle>();
				HSSFWorkbook hSSFWorkbook = new HSSFWorkbook();
				ISheet sheet = hSSFWorkbook.CreateSheet(sheetName);
				sheet.DefaultRowHeightInPoints = nopiCellConfig.RowHeight;
				sheet.DefaultColumnWidth = nopiCellConfig.ColWidth;
				IRow row = sheet.CreateRow(0);
				ICellStyle defualtStyle = hSSFWorkbook.CreateCellStyle();
				HSSFFont hSSFFont = (HSSFFont)hSSFWorkbook.CreateFont();
				if (nopiCellConfig.CellStyle == null)
				{
					defualtStyle.Alignment = HorizontalAlignment.Left;
					defualtStyle.VerticalAlignment = VerticalAlignment.Center;
					hSSFFont.Color = 18;
					defualtStyle.FillForegroundColor = 9;
					defualtStyle.WrapText = true;
					defualtStyle.SetFont(hSSFFont);
				}
				else
				{
					NpoiExcel.CellFormat(ref defualtStyle, hSSFFont, nopiCellConfig.CellStyle);
				}
				var list2 = (from x in typeof(T).GetProperties()
					select new
					{
						Property = x,
						PropertyName = x.Name,
						DescribeAb = (Attribute.GetCustomAttribute(x, typeof(DescriptionAttribute)) as DescriptionAttribute)
					} into x
					where x.DescribeAb != null
					select x).ToList();
				for (int i = 0; i < list2.Count; i++)
				{
					row.CreateCell(i, CellType.String).SetCellValue(list2[i].DescribeAb.Description);
					row.HeightInPoints = nopiCellConfig.RowHeight;
					row.GetCell(i).CellStyle = defualtStyle;
				}
				if (nopiCellConfig.IsMerged)
				{
					sheet.AddMergedRegion(new CellRangeAddress(nopiCellConfig.NopiMergedConfig.FirstRow, nopiCellConfig.NopiMergedConfig.LastCol, nopiCellConfig.NopiMergedConfig.FirstCol, nopiCellConfig.NopiMergedConfig.LastCol));
				}
				int num = 1;
				for (int j = 0; j < list.Count + num; j++)
				{
					IRow row2 = sheet.CreateRow(j);
					row2.HeightInPoints = nopiCellConfig.RowHeight;
					for (int k = 0; k < list2.Count; k++)
					{
						row2.CreateCell(k, CellType.String).SetCellValue((j == 0) ? list2[k].DescribeAb.Description : list2[k].Property.GetValue(list[j - num])?.ToString());
						NopiCellStyle nopiCellStyle = cellStyleCallBack?.Invoke(j, k);
						if (nopiCellStyle != null)
						{
							if (!dictionary.ContainsKey(nopiCellStyle.Index))
							{
								ICellStyle defualtStyle2 = hSSFWorkbook.CreateCellStyle();
								defualtStyle2.FillPattern = FillPattern.SolidForeground;
								HSSFFont defualtFont = (HSSFFont)hSSFWorkbook.CreateFont();
								NpoiExcel.CellFormat(ref defualtStyle2, defualtFont, nopiCellStyle);
								dictionary.Add(nopiCellStyle.Index, defualtStyle2);
							}
							row2.GetCell(k).CellStyle = dictionary[nopiCellStyle.Index];
						}
					}
					int num2 = j * 100 / list.Count;
					if (controlLogic.IsDataChange(num2))
					{
						calProcess?.Invoke(num2);
					}
				}
				if (img != null)
				{
					MemoryStream memoryStream = new MemoryStream();
					img.Save(memoryStream, ImageFormat.Gif);
					byte[] pictureData = memoryStream.ToArray();
					int pictureIndex = hSSFWorkbook.AddPicture(pictureData, PictureType.JPEG);
					HSSFPatriarch hSSFPatriarch = (HSSFPatriarch)sheet.CreateDrawingPatriarch();
					HSSFClientAnchor anchor = new HSSFClientAnchor(nopiCellConfig.NopiImgConfig.DxLeft, nopiCellConfig.NopiImgConfig.DyTop, nopiCellConfig.NopiImgConfig.DxRight, nopiCellConfig.NopiImgConfig.DyDown, nopiCellConfig.NopiImgConfig.StartColTop, nopiCellConfig.NopiImgConfig.StarRowTop, nopiCellConfig.NopiImgConfig.EndColDown, nopiCellConfig.NopiImgConfig.EndRowDown);
					HSSFPicture hSSFPicture = (HSSFPicture)hSSFPatriarch.CreatePicture(anchor, pictureIndex);
				}
				using (Stream @out = File.OpenWrite(filePath))
				{
					hSSFWorkbook.Write(@out);
				}
				calProcess?.Invoke(100);
				return true;
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}

		public static bool Export(DataTable dtExport, string filePath, string sheetName, Image img = null, NpoiCellConfig nopiCellConfig = null, Action<int> calProcess = null, Func<int, int, NopiCellStyle> cellStyleCallBack = null, bool useDtCaption = false)
		{
			try
			{
				if (nopiCellConfig == null)
				{
					nopiCellConfig = new NpoiCellConfig();
				}
				if (dtExport == null || dtExport.Rows.Count == 0)
				{
					return false;
				}
				ControlLogic<int> controlLogic = new ControlLogic<int>();
				Dictionary<int, ICellStyle> dictionary = new Dictionary<int, ICellStyle>();
				HSSFWorkbook hSSFWorkbook = new HSSFWorkbook();
				ISheet sheet = hSSFWorkbook.CreateSheet(sheetName);
				sheet.DefaultRowHeightInPoints = nopiCellConfig.RowHeight;
				sheet.DefaultColumnWidth = nopiCellConfig.ColWidth;
				IRow row = sheet.CreateRow(0);
				ICellStyle defualtStyle = hSSFWorkbook.CreateCellStyle();
				HSSFFont hSSFFont = (HSSFFont)hSSFWorkbook.CreateFont();
				if (nopiCellConfig.CellStyle == null)
				{
					defualtStyle.Alignment = HorizontalAlignment.Left;
					defualtStyle.VerticalAlignment = VerticalAlignment.Center;
					hSSFFont.Color = 18;
					hSSFFont.Color = 18;
					defualtStyle.FillForegroundColor = 9;
					hSSFFont.IsBold = true;
					defualtStyle.WrapText = true;
					defualtStyle.SetFont(hSSFFont);
				}
				else
				{
					NpoiExcel.CellFormat(ref defualtStyle, hSSFFont, nopiCellConfig.CellStyle);
				}
				for (int i = 0; i < dtExport.Columns.Count; i++)
				{
					string cellValue = (useDtCaption ? dtExport.Columns[i].Caption : dtExport.Columns[i].ColumnName);
					row.CreateCell(i, CellType.String).SetCellValue(cellValue);
					row.HeightInPoints = nopiCellConfig.RowHeight;
					row.GetCell(i).CellStyle = defualtStyle;
				}
				if (nopiCellConfig.IsMerged)
				{
					sheet.AddMergedRegion(new CellRangeAddress(nopiCellConfig.NopiMergedConfig.FirstRow, nopiCellConfig.NopiMergedConfig.LastCol, nopiCellConfig.NopiMergedConfig.FirstCol, nopiCellConfig.NopiMergedConfig.LastCol));
				}
				int num = 1;
				for (int j = 0; j < dtExport.Rows.Count + num; j++)
				{
					IRow row2 = sheet.CreateRow(j);
					row2.HeightInPoints = nopiCellConfig.RowHeight;
					for (int k = 0; k < dtExport.Columns.Count; k++)
					{
						string text = (useDtCaption ? dtExport.Columns[k].Caption : dtExport.Columns[k].ColumnName);
						row2.CreateCell(k, CellType.String).SetCellValue((j == 0) ? text : dtExport.Rows[j - 1][k].ToString());
						NopiCellStyle nopiCellStyle = cellStyleCallBack?.Invoke(j, k);
						if (nopiCellStyle != null)
						{
							if (!dictionary.ContainsKey(nopiCellStyle.Index))
							{
								ICellStyle defualtStyle2 = hSSFWorkbook.CreateCellStyle();
								defualtStyle2.FillPattern = FillPattern.SolidForeground;
								HSSFFont defualtFont = (HSSFFont)hSSFWorkbook.CreateFont();
								NpoiExcel.CellFormat(ref defualtStyle2, defualtFont, nopiCellStyle);
								dictionary.Add(nopiCellStyle.Index, defualtStyle2);
							}
							row2.GetCell(k).CellStyle = dictionary[nopiCellStyle.Index];
						}
					}
					int num2 = j * 100 / dtExport.Rows.Count;
					if (controlLogic.IsDataChange(num2))
					{
						calProcess?.Invoke(num2);
					}
				}
				if (img != null)
				{
					MemoryStream memoryStream = new MemoryStream();
					img.Save(memoryStream, ImageFormat.Gif);
					byte[] pictureData = memoryStream.ToArray();
					int pictureIndex = hSSFWorkbook.AddPicture(pictureData, PictureType.JPEG);
					HSSFPatriarch hSSFPatriarch = (HSSFPatriarch)sheet.CreateDrawingPatriarch();
					HSSFClientAnchor anchor = new HSSFClientAnchor(nopiCellConfig.NopiImgConfig.DxLeft, nopiCellConfig.NopiImgConfig.DyTop, nopiCellConfig.NopiImgConfig.DxRight, nopiCellConfig.NopiImgConfig.DyDown, nopiCellConfig.NopiImgConfig.StartColTop, nopiCellConfig.NopiImgConfig.StarRowTop, nopiCellConfig.NopiImgConfig.EndColDown, nopiCellConfig.NopiImgConfig.EndRowDown);
					HSSFPicture hSSFPicture = (HSSFPicture)hSSFPatriarch.CreatePicture(anchor, pictureIndex);
				}
				using (Stream @out = File.OpenWrite(filePath))
				{
					hSSFWorkbook.Write(@out);
				}
				calProcess?.Invoke(100);
				return true;
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}

		public static bool ExportMulti(List<DataTable> dtExport, string filePath, string sheetName, Image img = null, NpoiCellConfig nopiCellConfig = null, Action<int> calProcess = null, Func<int, int, NopiCellStyle> cellStyleCallBack = null, bool useDtCaption = false)
		{
			try
			{
				string text = sheetName;
				if (nopiCellConfig == null)
				{
					nopiCellConfig = new NpoiCellConfig();
				}
				if (dtExport == null || dtExport.Count == 0)
				{
					return false;
				}
				HSSFWorkbook hSSFWorkbook = new HSSFWorkbook();
				int num = dtExport.Sum((DataTable x) => x.Rows.Count);
				for (int i = 0; i < dtExport.Count; i++)
				{
					DataTable dataTable = dtExport[i];
					ControlLogic<int> controlLogic = new ControlLogic<int>();
					Dictionary<int, ICellStyle> dictionary = new Dictionary<int, ICellStyle>();
					string text2;
					if (dtExport.Count <= 0)
					{
						text2 = sheetName;
					}
					else
					{
						DefaultInterpolatedStringHandler defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(0, 2);
						defaultInterpolatedStringHandler.AppendFormatted(sheetName);
						defaultInterpolatedStringHandler.AppendFormatted(i + 1);
						text2 = defaultInterpolatedStringHandler.ToStringAndClear();
					}
					text = text2;
					ISheet sheet = hSSFWorkbook.CreateSheet(text);
					sheet.DefaultRowHeightInPoints = nopiCellConfig.RowHeight;
					sheet.DefaultColumnWidth = nopiCellConfig.ColWidth;
					IRow row = sheet.CreateRow(0);
					ICellStyle defualtStyle = hSSFWorkbook.CreateCellStyle();
					HSSFFont hSSFFont = (HSSFFont)hSSFWorkbook.CreateFont();
					if (nopiCellConfig.CellStyle == null)
					{
						defualtStyle.Alignment = HorizontalAlignment.Left;
						defualtStyle.VerticalAlignment = VerticalAlignment.Center;
						hSSFFont.Color = 18;
						hSSFFont.Color = 18;
						defualtStyle.FillForegroundColor = 9;
						hSSFFont.IsBold = true;
						defualtStyle.WrapText = true;
						defualtStyle.SetFont(hSSFFont);
					}
					else
					{
						NpoiExcel.CellFormat(ref defualtStyle, hSSFFont, nopiCellConfig.CellStyle);
					}
					for (int j = 0; j < dataTable.Columns.Count; j++)
					{
						string cellValue = (useDtCaption ? dataTable.Columns[j].Caption : dataTable.Columns[j].ColumnName);
						row.CreateCell(j, CellType.String).SetCellValue(cellValue);
						row.HeightInPoints = nopiCellConfig.RowHeight;
						row.GetCell(j).CellStyle = defualtStyle;
					}
					if (nopiCellConfig.IsMerged)
					{
						sheet.AddMergedRegion(new CellRangeAddress(nopiCellConfig.NopiMergedConfig.FirstRow, nopiCellConfig.NopiMergedConfig.LastCol, nopiCellConfig.NopiMergedConfig.FirstCol, nopiCellConfig.NopiMergedConfig.LastCol));
					}
					int num2 = 1;
					for (int k = 0; k < dataTable.Rows.Count + num2; k++)
					{
						IRow row2 = sheet.CreateRow(k);
						row2.HeightInPoints = nopiCellConfig.RowHeight;
						for (int l = 0; l < dataTable.Columns.Count; l++)
						{
							string text3 = (useDtCaption ? dataTable.Columns[l].Caption : dataTable.Columns[l].ColumnName);
							row2.CreateCell(l, CellType.String).SetCellValue((k == 0) ? text3 : dataTable.Rows[k - 1][l].ToString());
							NopiCellStyle nopiCellStyle = cellStyleCallBack?.Invoke(k, l);
							if (nopiCellStyle != null)
							{
								if (!dictionary.ContainsKey(nopiCellStyle.Index))
								{
									ICellStyle defualtStyle2 = hSSFWorkbook.CreateCellStyle();
									defualtStyle2.FillPattern = FillPattern.SolidForeground;
									HSSFFont defualtFont = (HSSFFont)hSSFWorkbook.CreateFont();
									NpoiExcel.CellFormat(ref defualtStyle2, defualtFont, nopiCellStyle);
									dictionary.Add(nopiCellStyle.Index, defualtStyle2);
								}
								row2.GetCell(l).CellStyle = dictionary[nopiCellStyle.Index];
							}
						}
						if (calProcess != null)
						{
							int num3 = k * 100 / num;
							if (controlLogic.IsDataChange(num3))
							{
								calProcess?.Invoke(num3);
							}
						}
					}
				}
				using (Stream @out = File.OpenWrite(filePath))
				{
					hSSFWorkbook.Write(@out);
				}
				calProcess?.Invoke(100);
				return true;
			}
			catch (Exception ex)
			{
				throw ex;
			}
		}

		private static void CellFormat(ref ICellStyle defualtStyle, HSSFFont defualtFont, NopiCellStyle cellStyle)
		{
			switch (cellStyle.FontColor)
			{
			case "Red":
				defualtFont.Color = 10;
				break;
			case "Green":
				defualtFont.Color = 17;
				break;
			case "Blue":
				defualtFont.Color = 12;
				break;
			case "Yellow":
				defualtFont.Color = 13;
				break;
			case "Black":
				defualtFont.Color = 8;
				break;
			case "Brown":
				defualtFont.Color = 60;
				break;
			default:
				defualtFont.Color = 8;
				break;
			}
			switch (cellStyle.BackColor)
			{
			case "Red":
				defualtStyle.FillForegroundColor = 10;
				break;
			case "Green":
				defualtStyle.FillForegroundColor = 17;
				break;
			case "Blue":
				defualtStyle.FillForegroundColor = 12;
				break;
			case "Yellow":
				defualtStyle.FillForegroundColor = 13;
				break;
			case "Black":
				defualtStyle.FillForegroundColor = 8;
				break;
			case "Brown":
				defualtStyle.FillForegroundColor = 60;
				break;
			default:
				defualtStyle.FillForegroundColor = 9;
				break;
			}
			if (cellStyle.FontSize > 0)
			{
				defualtFont.FontHeightInPoints = cellStyle.FontSize;
			}
			if (cellStyle.IsFontBold)
			{
				defualtFont.Boldweight = 700;
			}
			else
			{
				defualtFont.Boldweight = 0;
			}
			if (cellStyle.Underline == "Double")
			{
				defualtFont.Underline = FontUnderlineType.Double;
			}
			else if (cellStyle.Underline == "Single")
			{
				defualtFont.Underline = FontUnderlineType.Single;
			}
			else
			{
				defualtFont.Underline = FontUnderlineType.None;
			}
			defualtStyle.SetFont(defualtFont);
		}

		public static Dictionary<string, DataTable> ExcelToDataTables(string filePath, int startSheetIndex = 0, int fixedRowCount = 1)
		{
			Dictionary<string, DataTable> dictionary = new Dictionary<string, DataTable>();
			try
			{
				IWorkbook wrokBooK = NpoiExcel.GetWrokBooK(filePath);
				if (wrokBooK != null)
				{
					int numberOfSheets = wrokBooK.NumberOfSheets;
					if (startSheetIndex > numberOfSheets)
					{
						throw new Exception("指定起始表索引大于表单数量！");
					}
					for (int i = startSheetIndex; i < numberOfSheets; i++)
					{
						dictionary.Add(wrokBooK.GetSheetName(i), NpoiExcel.GetSheetDt(wrokBooK.GetSheetAt(i), fixedRowCount));
					}
				}
				return dictionary;
			}
			catch (Exception innerException)
			{
				throw new Exception("操作失败！", innerException);
			}
		}

		private static IWorkbook GetWrokBooK(string filePath)
		{
			IWorkbook result = null;
			FileStream fileStream = null;
			try
			{
				using (fileStream = File.OpenRead(filePath))
				{
					if (filePath.IndexOf(".xlsx", StringComparison.Ordinal) > 0)
					{
						result = new XSSFWorkbook((Stream)fileStream);
					}
					else if (filePath.IndexOf(".xls", StringComparison.Ordinal) > 0)
					{
						result = new HSSFWorkbook(fileStream);
					}
				}
				return result;
			}
			catch (Exception ex)
			{
				fileStream?.Close();
				throw ex;
			}
		}

		public static object GetCellValue(string filePath, int sheetIndex, int rowIndex, int colIndex)
		{
			ICell cell = ((NpoiExcel.GetWrokBooK(filePath)?.GetSheetAt(sheetIndex))?.GetRow(rowIndex))?.GetCell(colIndex);
			if (cell == null)
			{
				return null;
			}
			return NpoiExcel.CellTypeValueFormat(cell);
		}

		public static DataTable ExcelToDataTable(string filePath, int sheetIndex = 0, int fixedRowCount = 1)
		{
			try
			{
				IWorkbook wrokBooK = NpoiExcel.GetWrokBooK(filePath);
				if (wrokBooK != null)
				{
					return NpoiExcel.GetSheetDt(wrokBooK.GetSheetAt(sheetIndex), fixedRowCount);
				}
				return null;
			}
			catch (Exception innerException)
			{
				throw new Exception("操作失败", innerException);
			}
		}

		private static DataTable GetSheetDt(ISheet isheet, int fixedRowCount = 1)
		{
			if (isheet == null)
			{
				return new DataTable();
			}
			DataTable dataTable = new DataTable
			{
				TableName = isheet.SheetName
			};
			int lastRowNum = isheet.LastRowNum;
			if (lastRowNum > 0)
			{
				IRow row = isheet.GetRow(0);
				if (row == null)
				{
					return dataTable;
				}
				int lastCellNum = row.LastCellNum;
				if (fixedRowCount > 0)
				{
					for (int i = row.FirstCellNum; i < lastCellNum; i++)
					{
						ICell cell = row.GetCell(i);
						DataColumn column;
						if (!string.IsNullOrWhiteSpace(cell?.StringCellValue))
						{
							column = new DataColumn(cell.StringCellValue);
							dataTable.Columns.Add(column);
							continue;
						}
						DefaultInterpolatedStringHandler defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(8, 1);
						defaultInterpolatedStringHandler.AppendLiteral("BlankRow");
						defaultInterpolatedStringHandler.AppendFormatted(i);
						column = new DataColumn(defaultInterpolatedStringHandler.ToStringAndClear());
						dataTable.Columns.Add(column);
					}
				}
				else
				{
					for (int j = row.FirstCellNum; j < lastCellNum; j++)
					{
						DataColumn column = new DataColumn("column" + (j + 1));
						dataTable.Columns.Add(column);
					}
				}
				for (int k = fixedRowCount; k <= lastRowNum; k++)
				{
					IRow row2 = isheet.GetRow(k);
					if (row2 == null)
					{
						continue;
					}
					DataRow dataRow = dataTable.NewRow();
					for (int l = row2.FirstCellNum; l < lastCellNum; l++)
					{
						ICell cell = row2.GetCell(l);
						if (cell == null)
						{
							if (l > 0)
							{
								dataRow[l] = "";
							}
						}
						else
						{
							dataRow[l] = NpoiExcel.CellTypeValueFormat(cell);
						}
					}
					dataTable.Rows.Add(dataRow);
				}
			}
			return dataTable;
		}

		private static object CellTypeValueFormat(ICell cell)
		{
			object result = null;
			switch (cell.CellType)
			{
			case CellType.Blank:
				result = "";
				break;
			case CellType.Numeric:
			{
				short dataFormat = cell.CellStyle.DataFormat;
				result = ((dataFormat != 14 && dataFormat != 31 && dataFormat != 57 && dataFormat != 58) ? ((object)cell.NumericCellValue) : ((object)cell.DateCellValue));
				break;
			}
			case CellType.String:
				result = cell.StringCellValue;
				break;
			case CellType.Boolean:
				result = cell.BooleanCellValue;
				break;
			}
			return result;
		}
	}
}
